package simpleftp.server;

import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * Simple serveur TCP creant des thread pour gerer chaque connexion client.
 * 
 * @author lom
 */
public class FTPServerMain {

	/**
	 * Port d'ecoute.
	 */
	public static final int PORT = 7802;
	/**
	 * Socket serveur d'accueil.
	 */
	private static ServerSocket socketaccueil;

	public static void main(String argv[]) throws Exception {
		// MULTITHREAD
		int numberOfCores = Runtime.getRuntime().availableProcessors();
	    int poolSize = (int)(numberOfCores / (1 - 0.9));
		List<Future> futur = new ArrayList<Future>();
		ExecutorService executorPool = Executors.newScheduledThreadPool(poolSize);
		
		
		try {
			socketaccueil = new ServerSocket(PORT);
			System.out.println("Server up and running.");
			FTPServer.setDebug(true);
			while (true) {
				// add thread to executorPool
				futur.add( executorPool.submit(new FTPServer(socketaccueil.accept())) );
			}
		} finally {
			if (socketaccueil != null) {
				socketaccueil.close();
			}
		}
	}
}
